package com.wit.witmg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TFileLog;
import com.wit.witmg.mapper.TFileLogMapper;
import com.wit.witmg.service.ITFileLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import com.wit.witmg.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-05-06
 */
@Service
public class TFileLogServiceImpl extends ServiceImpl<TFileLogMapper, TFileLog> implements ITFileLogService {

    @Override
    public Object add(HttpServletRequest request, String code, String str) {
        TFileLog t=new TFileLog();
        t.setCode(code);
        t.setStr(str);
        t.setDate(new Date());
        if(save(t)){
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_504);
    }

    @Override
    public Object get(HttpServletRequest request, String code) {
        QueryWrapper<TFileLog> qw=new QueryWrapper<>();
        if(StringUtils.instance.isEmpty(code)){
            qw.orderByDesc("id");
        }else{
            qw.eq("code",code);
        }
        qw.last("limit 1");
        return JsonResult.success(getOne(qw));
    }
}
